/*
* Copyright 2010 Pavel Kozhin 
*
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed  under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License.
*
* Author: Pavel Kozhin.
*/

package org.bookmarksframework.navigation
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.bookmarksframework.browser.IBrowserAdapter;
	import org.bookmarksframework.browser.ISimpleHistory;
	import org.bookmarksframework.core.IDestroyable;
	import org.bookmarksframework.history.IHistory;
	
	public class NavigationHistory implements INavigationHistory
	{
		protected var simple:ISimpleHistory;
		
		protected var local:IHistory;
		
		public function NavigationHistory(simple:ISimpleHistory, local:IHistory)
		{
			this.simple = simple;
			this.local = local;
		}
		
		public function store(value:*):void
		{
			getLocalHistory().store(value);
		}
		
		public function get collection():ArrayCollection
		{
			return getLocalHistory().collection;
		}
		
		public function get current():*
		{
			return getLocalHistory().current;
		}
		
		public function get next():*
		{
			return getLocalHistory().next;
		}
		
		public function get previous():*
		{
			return getLocalHistory().previous;
		}
		
		public function destroy():void
		{
			if (local is IDestroyable)
				IDestroyable(local).destroy();
			
			if (simple is IDestroyable)
				IDestroyable(simple).destroy();
		}
		
		public function forward():void
		{
			getSimpleHistory().forward();
		}
		
		public function back():void
		{
			getSimpleHistory().back();
		}
		
		public function go(value:int):void
		{
			getSimpleHistory().go(value);
		}
		
		public function get length():Number
		{
			return getSimpleHistory().length;
		}
		
		public function addEventListener(type:String, listener:Function, useCapture:Boolean=false, 
										 priority:int=0, useWeakReference:Boolean=false):void
		{
			getSimpleHistory().addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		public function removeEventListener(type:String, listener:Function, useCapture:Boolean=false):void
		{
			getSimpleHistory().removeEventListener(type, listener, useCapture);
		}
		
		public function dispatchEvent(event:Event):Boolean
		{
			return getSimpleHistory().dispatchEvent(event);
		}
		
		public function hasEventListener(type:String):Boolean
		{
			return getSimpleHistory().hasEventListener(type);
		}
		
		public function willTrigger(type:String):Boolean
		{
			return getSimpleHistory().willTrigger(type);
		}
		
		protected function getSimpleHistory():ISimpleHistory
		{
			if (simple)
			{
				return simple;
			}
			else if (local)
			{
				return local;
			}
			else
			{
				throw new Error("NavigationHistory Error: ISimpleHistory " +
					"or IHistory should be initialized.");
			}
		}
		
		protected function getLocalHistory():IHistory
		{
			if (!local)
				throw new Error("NavigationHistory Error: IHistory is not initialized.");
			
			return local;
		}
	}
}